<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/2/8
 * Time: 下午3:47
 */

namespace app\leader\controller;


use app\common\model\WxOpen;
use app\common\model\XcxSet;
use think\Controller;
use Wechat\Wxobjopen;

class Xcxsysset extends Controller
{
    public function verset()
    {
        $XcxSet = new XcxSet();

        $options = $this->options();
        $wxObj = new Wxobjopen($options);
        $ss = $wxObj->xcx_sys_templ();
        $data = [];
        if ($ss['errcode'] == 0) {
            $data = $ss['draft_list'];
        }
        $data2 = [];
        $ss2 = $wxObj->xcx_tpl_list();
        if ($ss2['errcode'] == 0) {
            $data2 = $ss2['template_list'];
        }
        $xcxset = $XcxSet->find();
        $this->assign('xcxset', $xcxset);
        $this->assign('data', $data);
        $this->assign('data2', $data2);
        return $this->fetch();
    }

    public function addtpl()
    {
        if (request()->isAjax()) {
            $options = $this->options();
            $wxObj = new Wxobjopen($options);
            $param['draft_id'] = request()->post('draft_id');
            $ss = $wxObj->xcx_addtotpl($param);
            if ($ss['errcode'] == 0) {
                $data['code'] = 0;
                $data['message'] = "设置成功";
            } else {
                $data['code'] = $ss['errcode'];
                $data['message'] = $ss['errmsg'];
            }
            return json($data);
        }

    }

    public function set_tpl()
    {
        if (request()->isAjax()) {
            $data['template_id'] = request()->post('template_id');
            $data['extpages'] = request()->post('extpages');
            $data['user_version'] = request()->post('user_version');
            $data['weburl'] = request()->post('weburl');
            $data['user_desc'] = request()->post('user_desc');
            $WxXcxSet = new XcxSet();
            $up = $WxXcxSet->save($data, ['id' => 1]);
            if ($up) {
                $datas['code'] = 0;
                $datas['message'] = "设置成功";
            } else {
                $datas['code'] = 1;
                $datas['message'] = "设置失败";
            }
            return json($datas);
        }
    }

    public function del_tpl()
    {
        if (request()->isAjax()) {
            $options = $this->options();
            $wxObj = new Wxobjopen($options);
            $param['template_id'] = request()->post('template_id');
            $ss = $wxObj->xcx_tpl_del($param);
            if ($ss['errcode'] == 0) {
                $data['code'] = 0;
                $data['message'] = "删除成功";
            } else {
                $data['code'] = $ss['errcode'];
                $data['message'] = $ss['errmsg'];
            }
            return json($data);
        }


    }

    public function options()
    {
        $System = new \app\common\model\System();
        $config = $System->where(['id' => 1])->find();

        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
        );
        return $options;
    }

}